package de.hfu.android;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import de.hfu.helper.DataAccess;
import de.hfu.model.*;

public class WeekViewListAdapter extends BaseExpandableListAdapter {
	List<DayPlan> weekPlan;
	ItemFactory itemFactory;

	public WeekViewListAdapter(ItemFactory itemFactory) {
		super();
		this.itemFactory = itemFactory;
		this.weekPlan = new ArrayList<DayPlan>();
	}

	public void setData(List<DayPlan> menuPlan) {
		this.weekPlan = menuPlan;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return weekPlan.get(groupPosition).getDishes().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View view = null;

		Dish dish = (Dish) getChild(groupPosition, childPosition);		

		view = itemFactory.createDishView(dish, convertView, null);

		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		List<Dish> dishes = weekPlan.get(groupPosition).getDishes();
		
		if (dishes.get(dishes.size() - 1).getTitle().equalsIgnoreCase("Kennzeichnung"))
			return dishes.size() - 1;
		else
			return dishes.size();		
	}

	@Override
	public Object getGroup(int groupPosition) {
		return weekPlan.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return weekPlan.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		DayPlan day = (DayPlan) getGroup(groupPosition);

		View view = null;
		view = itemFactory.createDayItemView(day, convertView, null);
		return view;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	public Dish getKennzeichnung(int groupPosition) {
		List<Dish> dishes = weekPlan.get(groupPosition).getDishes();
		Dish tmp = dishes.get(dishes.size() - 1); 
		if (tmp.getTitle().equalsIgnoreCase("Kennzeichnung"))
			return tmp;
		return null;
	}

}
